﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace MxWeiXinPF.Web.bussiness.coupon
{
    public class CouponBussiness : IBussiness
    {
        private BLL.wx_shop_coupon bcoupon = new BLL.wx_shop_coupon();
        private Model.wx_shop_coupon mcoupon = new Model.wx_shop_coupon();
        private BLL.wx_shop_coupon_data bcoupondata = new BLL.wx_shop_coupon_data();
        public CouponBussiness()
        {
            //DataSet ds = bcoupon.GetListByPage(" status = 1 ", " id asc", 1, 1);
            //if (ds != null && ds.Tables[0].Rows.Count > 0)
            //{
            //    mcoupon = bcoupon.DataTableToList(ds.Tables[0])[0];
            //}
        }

        public void Process()
        {
            //ProcessSendCouponToUser();
            ProcessSendCouponMsg();
        }

        /// <summary>
        /// 给已送券的用户发送微信消息
        /// </summary>
        private void ProcessSendCouponMsg()
        {
            DataSet ds = new BLL.wx_shop_coupon_data().GetList(string.Format(" sendmsgstate = 0 and status = 0 "));
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return;
            }

            List<Model.wx_shop_coupon_data> models = new BLL.wx_shop_coupon_data().DataTableToList(ds.Tables[0]);
            foreach (Model.wx_shop_coupon_data item in models)
            {
                try
                {
                    mcoupon = new BLL.wx_shop_coupon().GetModel(item.couponid);
                    if (mcoupon == null || mcoupon.status == 0 || mcoupon.timeend < DateTime.Now)
                    {
                        continue;
                    }

                    string content = mcoupon.description;
                    string titile = mcoupon.couponname;
                    string picurl = Common.MyCommFun.getWebSite() + mcoupon.thumb;
                    WxMessageFunc.SendNewsMessage(item.openid, titile, content, picurl, "http://wx.9yyb.com/wxoauth.aspx?wid=39&p=3");
                    item.sendmsgstate = 1;
                    item.sendmsgtime = DateTime.Now;
                    new BLL.wx_shop_coupon_data().Update(item);
                }
                catch
                {
                }

            }
        }

        /// <summary>
        /// 给未领券的用户送券
        /// </summary>
        private void ProcessSendCouponToUser()
        {
            DataSet ds = new BLL.users().GetList("");
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return;
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int id = Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString());
                Model.wx_shop_coupon_data tempcoupon = bcoupondata.GetModel(string.Format("senduid = {0} and couponid={1} ", id, mcoupon.id));
                if (tempcoupon == null)
                {
                    Model.wx_shop_coupon_data thecoupon = new Model.wx_shop_coupon_data();
                    thecoupon.couponid = mcoupon.id;
                    thecoupon.shopid = Common.MyCommFun.GetMainShopID();
                    thecoupon.openid = ds.Tables[0].Rows[i]["wxOpenId"].ToString();
                    thecoupon.gettype = 0;
                    thecoupon.status = 0;
                    thecoupon.gettime = DateTime.Now;
                    thecoupon.usetime = DateTime.Now;
                    thecoupon.senduid = id;
                    thecoupon.ordersn = 0;
                    thecoupon.sendmsgstate = 0;
                    thecoupon.sendmsgtime = DateTime.Now;
                    if (bcoupondata.Add(thecoupon) > 0)
                    {

                        //送券成功,增加日志记录
                        Model.wx_shop_coupon_log log = new Model.wx_shop_coupon_log();
                        log.shopid = Common.MyCommFun.GetMainShopID();
                        log.openid = ds.Tables[0].Rows[i]["wxOpenId"].ToString();
                        log.times = DateTime.Now;
                        log.couponid = mcoupon.id;
                        log.orderid = 0;
                        log.explain = "后台赠送用户";
                        log.remark = " ";
                        log.exstr1 = " ";
                        new BLL.wx_shop_coupon_log().Add(log);
                    }
                }
            }
        }

        /// <summary>
        /// 给没有使用券的用户发送消息
        /// </summary>
        private void ProcessSendMsgByUserHasnotUseCoupon()
        {
           

        }

        /// <summary>
        /// 用户使用优惠券
        /// </summary>
        /// <param name="coupondataid"></param>
        /// <param name="orderid"></param>
        public  bool UseCoupon(int coupondataid, Model.orders morder)
        {
            BLL.wx_shop_coupon_data bc = new BLL.wx_shop_coupon_data();
            Model.wx_shop_coupon_data data = bc.GetModel(coupondataid);
            if (data != null)
            {
                data.shopid = morder.shopid;
                data.status = 1;
                data.usetime = DateTime.Now;
                data.ordersn = morder.id;
                if (bc.Update(data))
                {
                    LogCoupon(morder.shopid,"用户使用优惠券购物",morder.id,morder.openid);
                    return true;
                }
            }
            return false;
        }

        private void LogCoupon(int shopid,string explain,int orderid,string openid)
        {
            try
            {
                Model.wx_shop_coupon_log log = new Model.wx_shop_coupon_log();
                log.openid = openid;
                log.shopid = shopid;
                log.orderid = orderid;
                log.explain = explain;
                log.times = DateTime.Now;
                new BLL.wx_shop_coupon_log().Add(log);
            }
            catch (Exception ex)
            {
                Common.LogHelper.WriteLog(typeof(CouponBussiness), ex);
            }
        }
    }
}